package com.qire.manhua.view;

import android.content.Context;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qire.manhua.R;
import com.qire.manhua.view.animator.FirstBuyAnimationBinder;
import com.qire.common.utils.EquipmentInfoUtil;

import androidx.annotation.Nullable;

/**
 * 新人特惠入口图标控件
 */
public class FirstBuyCoinView extends LinearLayout {

    /**
     * 创建并插入到指定布局中去，改方法会创建一个首次充值浮层View并返回。
     * @param parent        将要注入的父控件View
     * @param countdownTime 创建时需要启动定时器的剩余时长，单位秒（s）
     * @param bottomMargin  注入后的底部距离
     * @return  返回创建的首次充值浮层View
     */
    public static FirstBuyCoinView insertTo(ViewGroup parent, long countdownTime, int bottomMargin) {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(-2, -2);
        layoutParams.gravity        = Gravity.RIGHT | Gravity.BOTTOM;
        layoutParams.rightMargin    = EquipmentInfoUtil.dip2px(12);
        layoutParams.bottomMargin   = bottomMargin;

        FirstBuyCoinView firstBuyCoinView = new FirstBuyCoinView(parent.getContext());
        parent.addView(firstBuyCoinView, layoutParams);
        firstBuyCoinView.startCountDown(countdownTime);
        return firstBuyCoinView;
    }

    /**
     * 将首次充值浮层View，注入到新的布局中去，其中会发生于现有View父控件割切的行为。
     * @param parent        将要注入的父控件View
     * @param bottomMargin  注入后的底部距离调整
     */
    public void injection(ViewGroup parent, int bottomMargin) {
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) getLayoutParams();
        layoutParams.bottomMargin = bottomMargin;

        removeFromParent();

        parent.addView(this, layoutParams);

        //NOTE:绑定动画，因为割接时会触发动画销毁方法，所以注入后需要重新绑定动画，重新起动动画效果
        animationBinder.bindView(iconImage);
    }

    /**
     * 将View从父控件中移除
     */
    public void removeFromParent() {
        ViewGroup parentView = (ViewGroup) getParent();
        if (parentView != null) {
            parentView.removeView(this);
        }
    }

    private FrameLayout iconLayout;
    private ImageView iconImage;
    private ImageView textImage;
    private TextView countdownText;

    private FirstBuyAnimationBinder animationBinder = new FirstBuyAnimationBinder();

    public FirstBuyCoinView(Context context) {
        super(context);
        init(context);
    }

    public FirstBuyCoinView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FirstBuyCoinView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        //NOTE:布局属性初始化
        FrameLayout.LayoutParams iconImageLP     = new FrameLayout.LayoutParams(-2, -2);
        FrameLayout.LayoutParams textImageLP     = new FrameLayout.LayoutParams(-2, -2);
        LayoutParams countdownTextLP = new LayoutParams(-2, -2);
        //NOTE:布局属性设置调整
        iconImageLP.gravity         = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        iconImageLP.bottomMargin    = EquipmentInfoUtil.dip2px(5);
        textImageLP.gravity         = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        countdownTextLP.topMargin   = EquipmentInfoUtil.dip2px(5);;
        //NOTE:初始化子控件
        iconLayout      = new FrameLayout(context);
        iconImage       = new ImageView(context);
        textImage       = new ImageView(context);
        countdownText   = new TextView(context);
        //NOTE:子控件属性设置调整
        iconImage.setImageResource(R.drawable.app_comic_reader_newcomer_discount_pic);
        textImage.setImageResource(R.drawable.app_comic_reader_newcomer_discount);
        iconLayout.addView(iconImage, iconImageLP);
        iconLayout.addView(textImage, textImageLP);

        countdownText.setText("59分49秒");
        countdownText.setTextSize(11);
        countdownText.setTextColor(Color.WHITE);
        countdownText.setPadding(21, 6, 21, 6);
        countdownText.setBackgroundResource(R.drawable.app_comic_reader_first_buy_buoy);
        countdownText.setLayoutParams(countdownTextLP);
        //NOTE:插入布局
        addView(iconLayout);
        addView(countdownText);
        setGravity(Gravity.CENTER);
        setOrientation(VERTICAL);
        //NOTE:绑定动画
        animationBinder.bindView(iconImage);
    }

    private void startCountDown(long countdownTime){
        new CountDownTimer(countdownTime * 1000, 1000) {
            long time = countdownTime;

            @Override
            public void onTick(long millisUntilFinished) {
                time--;
                countdownText.setText(String.format("%02d分%02d秒", time/60, time%60));
            }

            @Override
            public void onFinish() {
                setVisibility(GONE);
            }
        }.start();
    }
}
